package com.infore.siteManagement.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.constant.ResultConstants;
import com.infore.siteManagement.entity.StationClassify;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IStationClassifyRepository;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.QueryUtil;
import com.infore.siteManagement.select.QueryDispatcher;
import com.infore.siteManagement.service.IStationClassifyService;
import com.infore.siteManagement.util.ArrayUtils;
import com.infore.siteManagement.util.DateUtil;
import com.infore.siteManagement.util.MyBeanUtils;
import com.infore.siteManagement.util.Page;
import com.infore.siteManagement.util.PageUtil;
import com.infore.siteManagement.util.ResultEntity;

@Service
public class StationClassifyServiceImpl implements IStationClassifyService {

	@Autowired
	private  IStationClassifyRepository iStationClassifyRepository;
	@Autowired
	private QueryDispatcher queryDispatcher;
	
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		if (org.springframework.util.StringUtils.isEmpty((queryParamDto))) {//无任何条件查询
			List<StationClassify> list = iStationClassifyRepository.findAll();
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		//仅分页查询
		}else if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject()) && 
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getQueryCondition()) &&
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleCondition()) && 
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleSql())) {
			org.springframework.data.domain.Page<StationClassify> pageable = iStationClassifyRepository.findAll(queryParamDto.getPageObject().getPageRequest());
			Page<StationClassify> page = PageUtil.pageConvert(pageable);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		//只传属性不传字段的查询
		}else if (org.apache.commons.lang.StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) {
			return this.query(queryParamDto);
		}
		//指定字段查询
		return queryDispatcher.dispatcher(queryParamDto, StationClassify.class);
	}
	
	@SuppressWarnings("serial")
	public ResultEntity query(QueryParamDto queryParamDto) {
		//判断是否有条件再判断是否需要分页,不分页必须传null
		if (!org.springframework.util.StringUtils.isEmpty(queryParamDto)) {
			//需要分页
			org.springframework.data.domain.Page<StationClassify> pageAble =  iStationClassifyRepository.findAll(new Specification<StationClassify>() {
				public Predicate toPredicate(Root<StationClassify> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			}, queryParamDto.getPageObject().getPageRequest());
			Page<StationClassify> page = PageUtil.pageConvert(pageAble);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		}else {
			List<StationClassify> list = iStationClassifyRepository.findAll(new Specification<StationClassify>() {
				public Predicate toPredicate(Root<StationClassify> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			});
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		}
	}

	public ResultEntity delete(String ids) {
		List<String> idsList = ArrayUtils.stringToList(ids);
		iStationClassifyRepository.deleteByStationClassifyIdIn(idsList);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.DELETE_SUCCESS);
	}

	public ResultEntity edit(StationClassify stationClassify) {
		Date time = DateUtil.getSystemTime();
		stationClassify.setStationClassifyTime(time);
		if (StringUtils.isNotBlank(stationClassify.getStationClassifyId())) {
			StationClassify classify = iStationClassifyRepository.findById(stationClassify.getStationClassifyId()).orElse(null);
			if (classify != null) {
				MyBeanUtils.copyNonNullProperties(stationClassify, classify);
				stationClassify = classify;
			}else {
				return new ResultEntity(ResultStatus.ERROR,ResultConstants.NO_SUCH_OBJECT);
			}
		}
		try {
			iStationClassifyRepository.saveAndFlush(stationClassify);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultEntity(ResultStatus.SUCCESS,Constants.DEAL_SUCCESS);
	}

}
